#!/usr/bin/env python
##================
##gui example
##================
import pygame,sys
import lwpgt

#main screen variable
SCREEN=pygame.display.set_mode((640,480))
#init pygame
pygame.init()
pygame.key.set_repeat(300,50)
pygame.display.set_caption("gui example")
alpha=64

##game class
class guiexample01:
    ##init class 
    def __init__(self):
        #loading images
        self.im1=pygame.image.load("data/test.png")
        #creating main container
        self.maincontainer=lwpgt.gui.Container()
        #creating widgets
        self.textbox=lwpgt.gui.TextBox(text="Click here to edit..",textsize=15,position=(100,70))
        self.textbox1=lwpgt.gui.TextBox(text="Cursors to scroll image. z/x to change image alpha w/s/a/d to move",textsize=15,position=(0,0),color=(128,128,0), size=(640, 20))
        self.textbox2=lwpgt.gui.TextBox(text="Not Editable one",textsize=15,position=(100,100),color=(0,128,128),editable=False)
        self.imagebox=lwpgt.gui.ImageBox(image=self.im1,position=(120,120))
        #connect events to handlers
        self.textbox2.Connect(lwpgt.EVT_ONCLICK,lambda x:self.click(x))
        #setting widgets
        self.imagebox.SetImageAlpha(alpha)
        #add to container
        self.maincontainer.Add(self.textbox)
        self.maincontainer.Add(self.textbox1)
        self.maincontainer.Add(self.textbox2)
        self.maincontainer.Add(self.imagebox)
        
    ##update 
    def update(self):
        pass
    
    ##on click event method
    def click(self,widget):
        print "click EVENT"
        widget.text="Clicked!"
        
    ## redraw
    def redraw(self,screen):
        screen.fill((0,0,0))
        ##drawing here
        self.maincontainer.Redraw(screen)
        pygame.display.flip()
        
    ##events handler for example
    def events(self):
        global alpha
        for e in pygame.event.get():
            self.maincontainer.PollEvent(e)
            if e.type==pygame.QUIT:
                sys.exit()
            if e.type==pygame.KEYDOWN:
                if e.key==pygame.K_UP:self.imagebox.Scroll(0, -5)
                if e.key==pygame.K_DOWN:self.imagebox.Scroll(0, 5)
                if e.key==pygame.K_LEFT:self.imagebox.Scroll(-5, 0)
                if e.key==pygame.K_RIGHT:self.imagebox.Scroll(5, 0)
                if e.key==pygame.K_w:self.imagebox.Move(0, -5)
                if e.key==pygame.K_s:self.imagebox.Move(0, 5)
                if e.key==pygame.K_a:self.imagebox.Move(-5, 0)
                if e.key==pygame.K_d:self.imagebox.Move(5, 0)
                if e.key==pygame.K_z:alpha+=5
                if e.key==pygame.K_x:alpha-=5
                self.imagebox.SetImageAlpha(alpha)
        
    ##mainloop
    def mainloop(self):
        while 1:
            self.events()
            self.update()
            self.redraw(SCREEN)

if __name__=="__main__":
    guiexample01().mainloop()
